<template>
  <div style="width:100%;" class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.idCard" placeholder="身份证号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('handle.search') }}</el-button>
        <!-- <el-button  type="primary" @click="addOrUpdateHandle()">{{ $t('handle.add') }}</el-button>
        <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">{{ $t('handle.batchDelete') }}</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      ref="table"
      border
      height="500"
       style="width: 100%"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      >
      <el-table-column
        type="selection"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        header-align="center"
        prop="name"
        align="center"
        :label="$t('form.name')">
      </el-table-column>
      <el-table-column
        prop="identificationNumber"
        header-align="center"
        align="center"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="portraitId.photoUrl"
        header-align="center"
        align="center"
        label="图片">
         <template slot-scope="scope">
          <!-- <span style="color:red;">{{ scope.row.icon }}</span> -->
          <img :src="getPhoto(scope.row.portraitEntity)" style="width:50px;height:50px;" />
          <!-- {{ sexType(scope.row.portraitId) }} -->
          <!-- <img src="./momey.png" style="width:30%" /> -->
          <!-- <el-tag v-if="photoType === 1" size="small"><img :src="getPhoto(scope.row.portraitId)" style="width:50px;height:50px;border-radius:30px;" /></el-tag> -->
          <!-- <el-tag v-else-if="photoType === 2" size="small" type="info"><img :src="getPhoto(scope.row.portraitId)" style="width:50px;height:50px;border-radius:30px;" /></el-tag> -->
        </template>
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
       :label="$t('form.sex')">
      </el-table-column>
      <el-table-column
        prop="nation"
        header-align="center"
        align="center"
        label="民族">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        :label="$t('handle.handle')" >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="populationDetailHandle(scope.row.populationCoding)">{{ $t('handle.detail') }}</el-button>
          <el-button type="text" size="small"  v-if="isAuth('ic:device:permission')" @click="devicePermission(scope.row.populationCoding)">设备权限</el-button>
          <!-- <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.populationCoding)">{{ $t('handle.update') }}</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.populationCoding)">{{ $t('handle.delete') }}</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top:5px;">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <actual-population v-if="detailVisible" ref="actualpopulation"></actual-population>
    <device-permission v-if="devicePermissionVisible" ref="devicePermission"></device-permission>
  </div>
</template>

<script>
  import AddOrUpdate from './add-or-update.vue'
  import devicePermission from './devicePermisson.vue'
  import ActualPopulation from '../dialog/actualPopulationDetail.vue'
  import { getDictList,getPhotoUrl } from '@/utils'
  import { imgUrlHost } from '@/config/env'
  export default {
    data () {
      return {
        devicePermissionVisible:false,
        dataForm: {
          key: '',
          idCard: ''
        },
        photoType: 0,
        imgUrlHost:imgUrlHost,
        dataList: [],
        sexList: [],
        populationTypeList: [],
        typeDocumentList: [],
        populationCancellationTypeList: [],
        nationList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        detailVisible: false,
        imageurl: ''
      }
    },
    components: {
      'add-or-update':AddOrUpdate,
      'actual-population':ActualPopulation,
      'device-permission':devicePermission
    },
    filters: { // 局部过滤器
      // FormattingMoney: value => {
      //   getDictById(value)
      //   .then(res => {
      //     return res.data.dgBaseDictionary.text
      //   })
      //   .catch(err => {
      //     throw err
      //   })
      //   return '我是默认值'
      // }
    },
    computed: {
      getPhoto () {
        return function (type) {
          let photo = ''
          if(type === null){
            photo = '../../../static/img/photo.png'
          } else {
            if(type.photoUrl !== null) {
              photo = this.imgUrlHost + type.photoUrl
            } else if(type.photoBase64 !== null) {
              photo = 'data:image/png;base64,' + type.photoBase64
            }else{
              photo = '../../../static/img/photo.png'
            }

          }
          return photo
        }
      },
      // populationType () {
      //   return function (type) {
      //     return this.populationTypeList[type]
      //   }
      // },
      // nationType() {
      //   return function (type) {
      //     return this.nationList[type]
      //   }
      // },
      // typeDocument() {
      //   return function (type) {
      //     return this.typeDocumentList[type]
      //   }
      // },
      // populationCancellationType() {
      //   return function (type) {
      //     return this.populationCancellationTypeList[type]
      //   }
      // }
      regionId() {
        return this.$store.state.address.regionId;
      }
      // // 计算属性
      // computedTxt () {
      //   return function (value) {
      //     getDictById(value)
      //     .then(res => {
      //       return res.data.dgBaseDictionary.text
      //     })
      //     .catch(err => {
      //       throw err
      //     })
      //     return '我是默认值'
      //   }
      // }
    },
    created () {
      this.initData()
    },
    methods: {
      devicePermission(populationCoding){// 设备权限配置
        // alert("设备权限弹出框");
        this.devicePermissionVisible=true;
        this.$nextTick(() => {
          this.$refs.devicePermission.init(populationCoding)
        });

        // this.$nextTick(() => {
        //   this.$refs.actualpopulation.init(id)
        // })

      },
      adjust(){
        setTimeout(()=>{
          this.$refs['table'].doLayout()
        },100)
      },
      initParam (){
        this.pageIndex = 1;
        this.pageSize = 10
      },
      initData () {
         getDictList('sex').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.sexList[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('population_type').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.populationTypeList[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('type_document').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.typeDocumentList[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('nation').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.nationList[data.dictList[i].value] = data.dictList[i].label
          }
        })
        getDictList('population_cancellation_type').then(({data}) => {
          for(let i=0;i < data.dictList.length;i++){
            this.populationCancellationTypeList[data.dictList[i].value] = data.dictList[i].label
          }
          this.getDataList()
        })
      },
      // 获取数据列表
      getDataList () {
        let _this = this
        this.dataListLoading = true
        // this.getPhotoUrl()
        this.$http({
          // url: this.$http.adornUrl('/dgactualpopulation/list'),
          url: '/dg-standard-info/dgactualpopulation/list',
          method: 'post',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'name': this.dataForm.key,
            'idCard': this.dataForm.idCard,
            'regionId':this.regionId
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            // this.dataList = data.page.list
            data.page.list.map(function (item) {
              // getPhotoUrl(item.portraitId).then(({data}) => {
              //   if(data.dgBasicPortrait){
              //     item.portraitId = data.dgBasicPortrait.photoUrl
              //   }else{
              //     item.portraitId = '../../../static/img/photo.png'
              //   }
              // })
              item.identificationType = _this.typeDocumentList[item.identificationType]
              item.sex = _this.sexList[item.sex]
              item.nation = _this.nationList[item.nation]
              item.managementCategory= _this.populationTypeList[item.managementCategory]
              item.logoutCategory = _this.populationCancellationTypeList[item.logoutCategory]
              return item
            })
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id,detail) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,detail)
        })
      },

      populationDetailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.actualpopulation.init(id)
        })
      },
      // getPhotoUrl (photoId) {
      //   this.$http({
      //     url: this.$http.adornUrl('/dgbasicportrait/info/'+photoId),
      //     method: 'get'
      //   }).then(({data}) => {
      //     console.log(data)
      //     if (data.dgBasicPortrait!== null) {
      //       this.imageurl = data.dgBasicPortrait.photoUrl
      //     } else {
      //       this.imageurl = ''
      //     }
      //   })
      // },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.populationCoding
        })
        this.$confirm(`确定进行删除操作嘛?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            // url: this.$http.adornUrl('/dgactualpopulation/delete'),
             url: '/dg-standard-info/dgactualpopulation/delete',
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
